const logger = require('util/logger')
const mongoose = require('mongoose').set('debug', true)
const TABLE = 'db_ng_blog'
// const url = `mongodb://190.168.0.17:27017/${TABLE}`
const url = `mongodb://127.0.0.1:27017/${TABLE}`
mongoose.connect(url, { 
  useNewUrlParser:true,
  useCreateIndex: true,
  useUnifiedTopology: true
})

let db = mongoose.connection
mongoose.Promise = global.Promise // 防止Mongoose: mpromise 错误

db.on('error', function (err) {
  // console.log('数据库连接出错', err)
  logger.getLogger('default').trace('mongodb', '连接成功')
})

db.on('open', function () {
  // console.log('数据库连接成功')
  logger.getLogger('default').trace('mongodb', '连接成功')
})

db.on('disconnected', function () {
  // console.log('数据库连接断开')
  logger.getLogger('default').trace('mongodb', '连接成功')
})


module.exports = db
